package com.club.subject.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.club.subject.common.entity.Result;
import com.club.subject.convert.SubjectLabelDTOConverter;
import com.club.subject.domain.entity.SubjectLabelBO;
import com.club.subject.domain.service.SubjectLabelDomainService;
import com.club.subject.dto.SubjectLabelDTO;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {
    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    //新增标签
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectLabelDTO.getLabelName()),
                    "标签名称不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectLabelController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增标签失败");
        }
    }
    //修改标签
    @PostMapping("/update")
    public Result<Boolean> updateSubjectLabel(@RequestBody SubjectLabelDTO  subjectLabelDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectLabelController.update.dto{}", JSON.toJSON(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.update(subjectLabelBO);
            return  Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectLabelController.add.error{}",e.getMessage(),e);
            return Result.fail("更新标签失败");
        }
    }

    //删除标签
    @PostMapping("/delete")
    public Result<Boolean> deleteSubjectLabel(@RequestBody SubjectLabelDTO  subjectLabelDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectLabelController.delete.dto{}", JSON.toJSON(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.delete(subjectLabelBO);
            return  Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectLabelController.add.error{}",e.getMessage(),e);
            return Result.fail("删除标签失败");
        }
    }

    //查询分类下标签
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO  subjectLabelDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectLabelController.queryLabelByCategoryId.dto{}", JSON.toJSON(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类id不能为空");
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            List<SubjectLabelBO> resultList =subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelDTOConverter.INSTANCE.convertBOToLabelDTOList(resultList);
            return  Result.ok(subjectLabelDTOS);
        } catch (Exception e) {
            log.error("SubjectLabelController.queryLabelByCategoryId.error{}",e.getMessage(),e);
            return Result.fail("查询分类下标签失败");
        }
    }
}
